﻿using System.Xml.Linq;
using NJabber.Stream.Elements;
using NUnit.Framework;

namespace NJabber.Stream.Elements
{
    [TestFixture]
    public class StanzaErrorElementTest : ErrorElementTestBase
    {
        private string errorGeneratorName = "test-error-generator";
        private string errorName = "stanza-error-name";
        private string errorDescription = "stanza-error-description";
        private string errorDescriptionLang = "lang";

        public StanzaErrorElementTest() : base(XmppNamespaces.StanzaError, "", "")
        {
        }

        [Test]
        public void Write_NotEmptyErrorNameAndDescription_WritesCorrectXml()
        {
            StanzaError errorElement = new StanzaError(StanzaErrorTypes.Auth, errorName, errorDescription, errorDescriptionLang);
            XElement expectedElement = CreateStanzaErrorXElement("auth", errorName, errorDescription, errorDescriptionLang);
            VerifyWriteOutput(expectedElement, errorElement);
        }

        private XElement CreateStanzaErrorXElement(string errorType, string errorName, string errorDescription, string descriptionLang)
        {
            XElement element = CreateErrorXElement(errorName, errorDescription, descriptionLang);
            element.SetAttributeValue("type", errorType);
            return element;
        }

        [Test]
        public void Write_NotEmptyType_WritesCorrectXml()
        {
            string errorType = StanzaErrorTypes.Continue;
            StanzaError element = new StanzaError(errorType);
            XElement expectedElement = CreateStanzaErrorXElement("", errorType);
            VerifyWriteOutput(expectedElement, element);
        }

        private XElement CreateStanzaErrorXElement(string errorGeneratorName, string errorType)
        {
            XElement element = CreateXElement("error");
            if (!string.IsNullOrEmpty(errorGeneratorName)) element.SetAttributeValue("by", errorGeneratorName);
            if (!string.IsNullOrEmpty(errorType)) element.SetAttributeValue("type", errorType);
            return element;
        }

        [Test]
        public void Write_NotEmptyErrorGeneratorName_WritesCorrectXml()
        {
            StanzaError element = new StanzaError(StanzaErrorTypes.Auth);
            XElement expectedElement = CreateStanzaErrorXElement(errorGeneratorName, "auth");
            element.ErrorGeneratorName = errorGeneratorName;
            VerifyWriteOutput(expectedElement, element);
        }
    }
}
